﻿using System.Linq;
using System.Collections.Generic;
using INSE6260.OnlineBanking.Infrastructure;
using INSE6260.OnlineBanking.Model.Accounts;


namespace INSE6260.OnlineBanking.Repository.EF.Repositories
{
    
    public class AccountRepository : RepositoryBase<Account>, IAccountRepository
    {
        public AccountRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {

        }
        public List<Account> GetCustomerAccounts(int clientId)
        {
            return SourceQuery.Where(account => account.Client.Id == clientId && account.Status).ToList();
        }

        public Model.Clients.Client GetClientByAccountId(int accountId)
        {
            var account = GetObjectSet().FirstOrDefault(acc => acc.Id == accountId);
            return account != null ? account.Client : null;
        }
        public List<Account> GetAccountListByClientId(int clientId)
        {
            return GetObjectSet().Where(acc => acc.ClientId == clientId && acc.Status).ToList();
        }

        public Account GetAccountById(int accountId)
        {
            var account = SourceQuery.FirstOrDefault(acc => acc.Id == accountId);
            return account;
        }

        public Account GetAccounByAccountNumber(string accountNumber)
        {
            var account = SourceQuery.FirstOrDefault(acc => string.Equals(acc.AccountNo, accountNumber));
            return account;
        }

        
        public List<AccountSelectableItem> GetSourceWithdrawalAccounts(int? clientId)
        {
            var clist = (from acc in SourceQuery.OfType<CheckingAccount>()
                         where acc.ClientId == clientId && acc.Status
                         select acc).ToList();
            var res = new List<AccountSelectableItem>();
            foreach (var checkingAccount in clist)
                res.Add(new AccountSelectableItem { AccountId = checkingAccount.Id, AccountDesc = checkingAccount.AccountTypeDiscription + "-" + checkingAccount.AccountNo + "(" + checkingAccount.Balance + ")" });
            var slist = (from acc in SourceQuery.OfType<SavingAccount>()
                         where acc.ClientId == clientId && acc.Status
                         select acc).ToList();
            foreach (var checkingAccount in slist)
                res.Add(new AccountSelectableItem { AccountId = checkingAccount.Id, AccountDesc = checkingAccount.AccountTypeDiscription + "-" + checkingAccount.AccountNo+"("+checkingAccount.Balance+")" });
            return res;

        }

        public List<AccountSelectableItem> GetDestinationWithdrawalAccounts(int? clientId)
        {
            var clist = (from acc in SourceQuery.OfType<MoneyCardAccount>()
                         where acc.ClientId == clientId && acc.Status
                         select acc).ToList();
            var res = new List<AccountSelectableItem>();
            foreach (var checkingAccount in clist)
                res.Add(new AccountSelectableItem { AccountId = checkingAccount.Id, AccountDesc = checkingAccount.AccountTypeDiscription + "-" + checkingAccount.AccountNo + "(" + checkingAccount.Balance + ")" });
            return res;
        }
        public string GetAccountDesc(int accountId)
        {
            var account = SourceQuery.FirstOrDefault(acc => acc.Id == accountId && acc.Status);
            return account != null ? account.AccountTypeDiscription + "-" + account.AccountNo+"("+account.Balance+")" : string.Empty;
        }
        public List<AccountType > GetNoneExistAccountType(int clientId)
        {
            var accountType = new List<AccountType>
                                  {
                                      new AccountType {Id = 1, Desc = "Checking"},
                                      new AccountType {Id = 2, Desc = "Saving"},
                                      new AccountType {Id = 3, Desc = "Credit"}
                                  };
            var accounts = SourceQuery.Where(acc => acc.ClientId == clientId ).Select(acc => acc).ToList();
            var list = accounts.Select(account => account.AccountTypeDiscription).ToList();
            return accountType.Where(at => !list.Contains(at.Desc)).ToList();
        }


        
    }
}
